package cn.itcast.bos.service.transit.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillDao;
import cn.itcast.bos.dao.transit.TransitInfoDao;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.service.transit.TransitInfoService;

@Service
@Transactional
public class TranSitInfoServiceImp implements TransitInfoService {


	@Override
	public void createtransitInfo(TransitInfo model, String waybillIds) {

		if (waybillIds != null) {
			String[] ids = waybillIds.split(",");
			for (String id : ids) {
				WayBill wayBill = wayBillDAO.findOne(Integer.parseInt(id));
				if (wayBill.getSignStatus() == 1) {
					TransitInfo transitInfo = new TransitInfo();
					transitInfo.setWayBill(wayBill);
					transitInfo.setStatus("出入库中转");

					// 更改运单状态
					wayBill.setSignStatus(2);
					//保存到数据库
					transitInfoDao.save(transitInfo);
					// 同步索引库
					wayIndexDao.save(wayBill);
				}

			}

		}

	}

	@Autowired
	private WayBillDao wayBillDAO;

	@Autowired
	private TransitInfoDao transitInfoDao;

	@Autowired
	private WayBillIndexRepository wayIndexDao;

	@Override
	public Page<TransitInfo> findAll(Pageable pageable) {
		Page<TransitInfo> page = transitInfoDao.findAll(pageable);

		return page;
	}

}
